package ru.amse.podolsky.fractalarch.model;

/**
 * @author Алексей
 */

import ru.amse.podolsky.fractalarch.ui.textui.TextOutput;
import ru.amse.podolsky.fractalarch.io.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.JOptionPane;

public class Actions {
    
    // <editor-fold defaultstate="collapsed" desc="Compressions">
    public static void doCompression(String[] args){
        if (args[0].charAt(0) == '-'){
            try{
                TextOutput.compress();
                ImagesIO inputImg = new ImagesIO(args[1]);
                Compression compression = new Compression(inputImg.read(), args[3]);
                DomenParams[] toWrite = compression.compress();
                IArchivIO arch = new BitArchivIO(args[2]);
                arch.write(toWrite, compression.getNumberOfDomens(), compression.getMiddleColor(), compression.getImageWidth(), compression.getImageHeight(), args[3]);
                TextOutput.compressDone();
            }catch(IOException e){
                TextOutput.error(e);
            }
        }else{
            TextOutput.incorrectParams();
            System.exit(0);
        }
    }

     public static void doTextCompression(String[] args){
        if (args[0].charAt(0) == '-'){
            try{
                TextOutput.compress();
                ImagesIO inputImg = new ImagesIO(args[1]);
                Compression compression = new Compression(inputImg.read(), args[3]);
                DomenParams[] toWrite = compression.compress();
                IArchivIO arch = new TextArchivIO(args[2]);
                arch.write(toWrite, compression.getNumberOfDomens(), compression.getMiddleColor(), compression.getImageWidth(), compression.getImageHeight(), args[3]);
                TextOutput.compressDone();
            }catch(IOException e){
                TextOutput.error(e);
            }
        }else{
            TextOutput.incorrectParams();
            System.exit(0);
        }
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Decompressions">
     public static void doDecompression(String[] args){
        if(args[0].charAt(0) == '-'){
            try{
                if(!isBitArchiv(args[1])){
                    doTextDecompression(args);
                    return;
                }
                TextOutput.decompress();
                IArchivIO arch = new BitArchivIO(args[1]);
                ArrayExtractor extractor = new ArrayExtractor(arch.read());
                Compression compression = extractor.extractCompressFromArray();
                BufferedImage imageToWrite = compression.decompress(extractor.extractParamsFromArray(), extractor.extractStepFromArray());
                ImagesIO outputImg = new ImagesIO(args[2]);
                outputImg.write(imageToWrite);
                TextOutput.decompressDone();
            }catch(IOException e){
                TextOutput.error(e);
            }
        }else{
            TextOutput.incorrectParams();
            System.exit(0);
        }
    }

    public static void doTextDecompression(String[] args){
        if(args[0].charAt(0) == '-'){
            try{
                if(isBitArchiv(args[1])){
                    doDecompression(args);
                    return;
                }
                TextOutput.decompress();
                IArchivIO arch = new TextArchivIO(args[1]);
                ArrayExtractor extractor = new ArrayExtractor(arch.read());
                Compression compression = extractor.extractCompressFromArray();
                BufferedImage imageToWrite = compression.decompress(extractor.extractParamsFromArray(), extractor.extractStepFromArray());
                ImagesIO outputImg = new ImagesIO(args[2]);
                outputImg.write(imageToWrite);
                TextOutput.decompressDone();
            }catch(IOException e){
                TextOutput.error(e);
            }
        }else{
            TextOutput.incorrectParams();
            System.exit(0);
        }
     }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Viewer">
    public static void doCompressionToViewer(String path, BufferedImage buff){
        try{
            Compression compression = new Compression(buff, IParams.DEFAULT_STEP);
            DomenParams[] toWrite = compression.compress();
            IArchivIO arch = new BitArchivIO(path);
            arch.write(toWrite, compression.getNumberOfDomens(), compression.getMiddleColor(), compression.getImageWidth(), compression.getImageHeight(), IParams.DEFAULT_STEP);
        }catch(IOException e){
            JOptionPane.showMessageDialog(null, "The following exception has been occured: " + e.getMessage(), "Warning", JOptionPane.WARNING_MESSAGE);
        }
    }
    
    public static BufferedImage doTextDecompressionToViewer(String path){
        BufferedImage imageToWrite = null;
        try{
            if(isBitArchiv(path)){
                return doDecompressionToViewer(path);
            }
            IArchivIO arch = new TextArchivIO(path);
            ArrayExtractor extractor = new ArrayExtractor(arch.read());
            Compression compression = extractor.extractCompressFromArray();
            imageToWrite = compression.decompress(extractor.extractParamsFromArray(), extractor.extractStepFromArray());
        }catch(IOException e){
            JOptionPane.showMessageDialog(null, "The following exception has been occured: " + e.getMessage(), "Warning", JOptionPane.WARNING_MESSAGE);
        }
        return imageToWrite;
     }

    public static BufferedImage doDecompressionToViewer(String path){
        BufferedImage imageToWrite = null;
        try{
            if(!isBitArchiv(path)){
                return doTextDecompressionToViewer(path);
            }
            IArchivIO arch = new BitArchivIO(path);
            ArrayExtractor extractor = new ArrayExtractor(arch.read());
            Compression compression = extractor.extractCompressFromArray();
            imageToWrite = compression.decompress(extractor.extractParamsFromArray(), extractor.extractStepFromArray());
        }catch(IOException e){
            JOptionPane.showMessageDialog(null, "The following exception has been occured: " + e.getMessage(), "Warning", JOptionPane.WARNING_MESSAGE);
        }
        return imageToWrite;
    }
    
    public static void doSaveAsImageToViewer(String path, BufferedImage buff){
        try{
            ImagesIO outputImg = new ImagesIO(path);
            outputImg.writeFormated(buff);
        }catch(IOException e){
            JOptionPane.showMessageDialog(null, "The following exception has been occured: " + e.getMessage(), "Warning", JOptionPane.WARNING_MESSAGE);
        }
    }

    public static void doSaveAsFractalToViewer(String pathIn, String pathOut){
        try{
            FilesIO.SaveFile(pathIn, pathOut);
        }catch(IOException e){
            JOptionPane.showMessageDialog(null, "The following exception has been occured: " + e.getMessage(), "Warning", JOptionPane.WARNING_MESSAGE);
        }
    }
    // </editor-fold>
    
    public static void showHelp(String[] args){
        if (args[0].charAt(0) == '-'){
                   TextOutput.help();
        }else{
            TextOutput.incorrectParams();
            System.exit(0);
        }
    }

    private static boolean isBitArchiv(String path) throws FileNotFoundException, IOException{
        DataInputStream input = new DataInputStream(new FileInputStream(path));
        int  param = input.readByte();
        if(param == 1){
            return true;
        }
        return false;
    }
}
